import { _decorator } from 'cc'
import { CONTROLLER_ENUM, DIRECTION_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM } from '../Enums'
import { EventManager } from '../RunTime/EventManager'
import { PlayerStateMachine } from './PlayerStateMachine'
import { EntityManager } from '../Base/EntityManager'
import DataManager from '../RunTime/DataManager'
const { ccclass, property } = _decorator

@ccclass('PlayerManager')
export class PlayerManager extends EntityManager {
  destinationX: number = 0
  destinationY: number = 0
  private readonly speed = 1 / 10

  inputHandle(inputDirection: CONTROLLER_ENUM) {
    let action = this.willBlock(inputDirection)
    if (action) {
      console.log('被瓦片阻挡,无法转身:', action)
      return
    }

    this.move(inputDirection)
  }

  willBlock(inputDirection: CONTROLLER_ENUM): boolean {
    const { destinationX: x, destinationY: y, direction } = this
    const { tileInfo } = DataManager.getInstance(DataManager)

    console.log('destinationX:', x, 'destinationY:', y)

    if (inputDirection === CONTROLLER_ENUM.TOP) {
      if (direction === DIRECTION_ENUM.TOP) {
        const playerNextY = y - 1
        const weaponNextY = y - 2

        console.log('playerNextY:', playerNextY)
        if (playerNextY < 0) {
          return true
        }

        const playerTile = tileInfo[x][playerNextY]
        const weaponTile = tileInfo[x][weaponNextY]

        if (playerTile && playerTile.moveAble && (!weaponTile || weaponTile.turnAble)) {
          // 可以转身
        } else {
          console.log('不可以转身01')
          return true
        }
      }
    } else if (inputDirection === CONTROLLER_ENUM.TURNLEFT) {
      let nextX: number
      let nextY: number

      if (direction === DIRECTION_ENUM.TOP) {
        nextX = x - 1
        nextY = y - 1
      } else if (direction === DIRECTION_ENUM.BOTTOM) {
        nextX = x + 1
        nextY = y + 1
      } else if (direction === DIRECTION_ENUM.LEFT) {
        nextX = x - 1
        nextY = y + 1
      } else if (direction === DIRECTION_ENUM.RIGHT) {
        nextX = x + 1
        nextY = y - 1
      }

      console.log(tileInfo)
      console.log('nextX=' + nextX, 'nextY=' + nextY)
      if (
        (!tileInfo[x][nextY] || tileInfo[x][nextY].turnAble) &&
        (!tileInfo[nextX][y] || tileInfo[nextX][y].turnAble) &&
        (!tileInfo[nextX][nextY] || tileInfo[nextX][nextY].turnAble)
      ) {
        // 可以转身
      } else {
        console.log('不可以转身02')
        return true
      }
    }

    return false
  }

  move(inputDirection: CONTROLLER_ENUM) {
    const dataInst = DataManager.getInstance(DataManager)
    console.log('dataInst.tileInfo', dataInst.tileInfo)

    if (inputDirection === CONTROLLER_ENUM.TOP) {
      this.destinationY -= 1
    } else if (inputDirection === CONTROLLER_ENUM.BOTTOM) {
      this.destinationY += 1
    } else if (inputDirection === CONTROLLER_ENUM.LEFT) {
      this.destinationX -= 1
    } else if (inputDirection === CONTROLLER_ENUM.RIGHT) {
      this.destinationX += 1
    } else if (inputDirection === CONTROLLER_ENUM.TURNLEFT) {
      if (this.direction === DIRECTION_ENUM.TOP) {
        this.direction = DIRECTION_ENUM.LEFT
      } else if (this.direction === DIRECTION_ENUM.LEFT) {
        this.direction = DIRECTION_ENUM.BOTTOM
      } else if (this.direction === DIRECTION_ENUM.BOTTOM) {
        this.direction = DIRECTION_ENUM.RIGHT
      } else if (this.direction === DIRECTION_ENUM.RIGHT) {
        this.direction = DIRECTION_ENUM.TOP
      }
      this.state = ENTITY_STATE_ENUM.TURNLEFT
    }
  }

  updateXY() {
    if (this.destinationX < this.currentX) {
      this.currentX -= this.speed
    } else if (this.destinationX > this.currentX) {
      this.currentX += this.speed
    }

    if (this.destinationY < this.currentY) {
      this.currentY -= this.speed
    } else if (this.destinationY > this.currentY) {
      this.currentY += this.speed
    }

    if (Math.abs(this.destinationX - this.currentX) <= 0.1) {
      if (Math.abs(this.destinationY - this.currentY) <= 0.1) {
        this.currentX = this.destinationX
        this.currentY = this.destinationY
      }
    }
  }

  async init() {
    this.fsm = this.addComponent(PlayerStateMachine)
    await this.fsm.init()

    super.init({
      x: 2,
      y: 8,
      type: ENTITY_TYPE_ENUM.PLAYER,
      direction: DIRECTION_ENUM.TOP,
      state: ENTITY_STATE_ENUM.IDLE,
    })

    const instance = EventManager.getInstance(EventManager)
    instance.on(EVENT_ENUM.PLAYER_CTRL, this.inputHandle, this)
  }

  update(): void {
    this.updateXY()
    super.update()
  }
}
